var dndSlideSorter = {}
dndSlideSorter.dragging = null;
dndSlideSorter.slidesorter= null;
dndSlideSorter.cells = null;

dndSlideSorter.isDraggable = function(node) {
    return hasClassName(node, 'sortable-cell');
};

dndSlideSorter.doDown = function(e) {
    if (!e) var e = window.event; // IE compatibility
    var target = this
    if (target == null)
        return;
    for (var i=0; i<dndSlideSorter.cells.length; i++)
        dndSlideSorter.cells[i].onmousemove = dndSlideSorter.doDrag;
    dndSlideSorter.dragging = target;
    dndSlideSorter.dragging._position = dndSlideSorter.getPos(dndSlideSorter.dragging);
    addClassName(dndSlideSorter.dragging, "dragging");
    return false;
}

dndSlideSorter.getPos = function(node) {
    var children = node.parentNode.childNodes;
    var pos = 0;
    for (var i=0; i<children.length; i++) {
        if (node == children[i])
            return pos;
        if (hasClassName(children[i], "sortable-cell"))
            pos++;
    }
    return null;
}

dndSlideSorter.doDrag = function(e) {
    if (!e) var e = window.event; // IE compatibility
    if (!dndSlideSorter.dragging)
        return;
    var target = this;//findContainer(e.target, dndSlideSorter.isDraggable);
    if (!target)
        return;
    if (target.id != dndSlideSorter.dragging.id) {
        dndSlideSorter.insertElement(target, dndSlideSorter.dragging);
    }
    return false;
}


dndSlideSorter.insertElement = function(child1, child2) {
    // child1 = target (current location of cursor)
    // child2 = item being dragged
    var parent = child1.parentNode;
    var children = parent.childNodes;
    var items = new Array();
    // get index of target and dragged item
    var target_pos, drag_pos = 0;
    for (var i = 0; i < children.length; i++) {
        var node = children[i];
        items[i] = node
        if (node.id) {
            removeClassName(node, "even");
            removeClassName(node, "odd");
            if (node.id == child1.id)
                target_pos = i;
            if (node.id == child2.id)
                drag_pos = i;
        }
    }
    // move dragged item to index of target (don't swap, just insert)
    // swapping meant that with new layout, index 1 could get swapped with
    // 7 and images visually jump around rather than insert
    if (drag_pos > target_pos)
        {
        // insert dragged
        items.splice(target_pos,0,items[drag_pos]);
        // delete original
        items.splice(drag_pos+1,1);
        }
    else{
        // insert dragged
        items.splice(target_pos+1,0,items[drag_pos]);
        // delete original
        items.splice(drag_pos,1);
        }

    Sarissa.clearChildNodes(parent);
    var pos = 0;
    for (var i = 0; i < items.length; i++) {
        var node = parent.appendChild(items[i]);
        if (node.id) {
            if (pos % 2)
                addClassName(node, "even");
            else
                addClassName(node, "odd");
            pos++;
        }
    }
}


dndSlideSorter.doUp = function(e) {
    if (!e) var e = window.event; // IE compatibility
    if (!dndSlideSorter.dragging)
        return;
    removeClassName(dndSlideSorter.dragging, "dragging");
    dndSlideSorter.updatePositionOnServer();
    dndSlideSorter.dragging._position = null;
    try {
        delete dndSlideSorter.dragging._position;
    } catch(e) {}
    dndSlideSorter.dragging = null;
    for (var i=0; i<dndSlideSorter.cells.length; i++)
        dndSlideSorter.cells[i].onmousemove = null;
    return false;
}
dndSlideSorter.updatePositionOnServer = function() {
    var delta = dndSlideSorter.getPos(dndSlideSorter.dragging) - dndSlideSorter.dragging._position;

    if (delta == 0) // nothing changed
        return;
    var req = new XMLHttpRequest();
    req.open("POST", "folder_moveitem", true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // Strip off id prefix
    var item_id = dndSlideSorter.dragging.id.substr('folder-contents-item-'.length);
    req.send("item_id="+item_id+"&delta:int="+delta);
}

dndSlideSorter.initializeDragDrop = function() {
    dndSlideSorter.slidesorter = cssQuery("#slide-sortable")[0];
    if (!dndSlideSorter.slidesorter)
        return;
    dndSlideSorter.cells= cssQuery("#slide-sortable > .sortable-cell");
    var targets = cssQuery("#slide-sortable > .sortable-cell");
    for (var i=0; i<targets.length; i++) {
        if (hasClassName(targets[i], 'notSortable-cell'))
            continue;
        targets[i].onmousedown=dndSlideSorter.doDown;
        targets[i].onmouseup=dndSlideSorter.doUp;
        addClassName(targets[i], "draggingHook");
    }
}

registerPloneFunction(dndSlideSorter.initializeDragDrop);




